/***
* This do file plots the evolution of small business revenue at the national level using Womply data. 
***/

*-------------------------------------------------------------------------------
* Setup 
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning) == 0) include "${root}/code/config_interactive.do"

* Set globals
include "${root}/code/set_globals.do"
local category "Small Business Revenue"

* Create required subfolders
cap mkdir "${root}/results"
cap mkdir "${root}/results/Small Business Revenue"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------
project, uses("${root}/data/web/data/Womply - National - Weekly.csv")
import delimited  "${root}/data/web/data/Womply - National - Weekly.csv", clear

* Plot range: Jan 1 2020 to Dec 31 2021
gen date = mdy(month, day_endofweek, year)
format %td date
keep if inrange(date, mdy(1, 1, 2020), mdy(12, 31, 2021))

* revenue_all is relative to Jan 2020. Express it in percentages.
replace revenue_all = revenue_all * 100

* Obtain x-axis labels
project, uses("${root}/code/ado/xlab_months.ado") raw
xlab_months, startmonth(Jan 2020) endmonth(Nov 2021) space(2)

line revenue_all date, ///
	ytitle("Change in Small Business Revenue (%)" "Relative to January 2020") ///
	xtitle("") ///
	ylab(20 "+20%" 10 "+10%" 0 "0%" -10 "-10%" -20 "-20%" -30 "-30%" -40 "-40%" -50 "-50%", nogrid) ///
	xlab( `r(xlab_months)', labsize(small)) ///
	yline(0, lcolor(gs8) lpattern(dash)) ///
	xsize(10)

oi_graph_export "${root}/results/Small Business Revenue/Small Business Revenue - National Revenue over time", type(${fig_type})

* Get small business revenue level in August 2020
sum revenue_all if date == mdy(8, 2, 2020)
local rev_aug2020 = `r(mean)'

* Get small business revenue level in April 2020
sum revenue_all if date == mdy(4, 5, 2020)
local rev_apr2020 = abs(`r(mean)')

* Get small business revenue level in July 2020
sum revenue_all if month == 7 & year == 2020
local rev_jul2020 = abs(`r(mean)')

cap erase "${root}/results/paper numbers/`category'/Evolution of revenue at the national level.yaml"

yamlout using "${root}/results/paper numbers/`category'/Evolution of revenue at the national level.yaml", ///
		key("rev_aug2020") ///
		comment("Small Business Revenue level in August 2020") ///
		value(`rev_aug2020') fmt(%9.0f)
		
yamlout using "${root}/results/paper numbers/`category'/Evolution of revenue at the national level.yaml", ///
		key("rev_apr2020") ///
		comment("Small Business Revenue level in April 2020") ///
		value(`rev_apr2020') fmt(%9.0f)	
		
yamlout using "${root}/results/paper numbers/`category'/Evolution of revenue at the national level.yaml", ///
		key("rev_jul2020") ///
		comment("Small Business Revenue level in July 2020") ///
		value(`rev_jul2020') fmt(%9.0f)		
		
project, uses("${root}/results/paper numbers/`category'/Evolution of revenue at the national level.yaml")
